<template>
  <div class="app-container">
    <el-row :gutter="40">
      <el-col :sm="24">
        <el-button
          type="primary"
          style="float: right"
          @click="dialogVisible = true"
          >添加管理员</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="listLoading"
      :data="list"
      style="margin-top: 30px"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column align="center" label="手机号">
        <template slot-scope="scope">{{ scope.row.username }}</template>
      </el-table-column>
      <el-table-column label="密码" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.password }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="created_at"
        label="操作"
        width="250"
      >
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="upAdmin(scope.row.id)"
            >编辑</el-button
          >
          <el-button type="danger" size="mini" @click="deleteUser(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="添加用户"
      @close="diaClose"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form :model="form">
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="用户密码">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdmin">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import PanelGroup from "@/components/PanelGroup";
import {
  addAdmin,
  adminList,
  updateAdmin,
  delAdmin,
  getAdminInfo,
} from "@/api/admin";
export default {
  data() {
    return {
      list: null,
      listLoading: true,
      dialogVisible: false,
      isAdd: true,
      userId: 0,
      form: {
        username: "",
        password: "",
      },
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      let res = await adminList();
      this.list = res.data;
      this.listLoading = false;
    },
    // 添加用户 || 修改用户
    async handleAdmin() {
      let bool = false;
      Object.keys(this.form).forEach((key) => {
        if (!this.form[key].length > 0) {
          bool = true;
        }
      });
      if (bool) {
        this.$message({
          message: "请检查用户信息是否填写完整",
          type: "warning",
        });
      } else {
        if (this.isAdd) {
          let result = await addAdmin(this.form);
          if (result.code == 200) {
            this.$message({
              message: result.msg,
              type: "success",
            });
            this.fetchData(this.page);
            this.dialogVisible = false;
          } else {
            this.$message({
              message: result.msg,
              type: "warning",
            });
          }
        } else {
          // this.dialogVisible = true;
          let result = await updateAdmin({ id: this.userId, ...this.form });
          if (result.code == 200) {
            this.$message({
              message: result.msg,
              type: "success",
            });
            this.fetchData(this.page);
            this.dialogVisible = false;
          } else {
            this.$message({
              message: result.msg,
              type: "warning",
            });
          }
        }
      }
    },
    deleteUser(id) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        let result = await delAdmin({ id });
        if (result.code == 200) {
          this.$message({
            type: "success",
            message: result.msg,
          });
          this.fetchData();
        } else {
          this.$message({
            type: "error",
            message: result.msg,
          });
        }
      });
    },
    async upAdmin(id) {
      this.userId = id;
      this.isAdd = false;
      let userinfo = await getAdminInfo({ id });
      this.form = {
        username: userinfo.data[0].username,
        password: userinfo.data[0].password,
      };
      this.dialogVisible = true;
    },
    diaClose() {
      this.form = {
        username: "",
        password: "",
      };
    },
  },
  components: {
    PanelGroup,
  },
};
</script>
